﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace TestReatXML
{

    public partial class FrmSettings : Form
    {

        public FrmSettings()
        {
            InitializeComponent();
        }

        private void callMethode(object sender)
        {
            if (sender == null)
                return;
            if (sender.Equals(btnDBConn))
            {

                MSDASC.DataLinks dataLinks = new MSDASC.DataLinks();
                ADODB._Connection connection;
                //are we editing an existing connect string or getting a new one?
                String connectionString = this.tDBConn.Text;
                if (connectionString == String.Empty)
                {
                    try
                    {
                        connection = (ADODB._Connection)dataLinks.PromptNew();
                        connectionString =
                             connection.ConnectionString.ToString();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else
                {
                    // edit connection string
                    connection = new ADODB.Connection();
                    connection.ConnectionString = connectionString;
                    //set local COM compatible data type
                    object oConnection = connection;
                    try
                    {
                        //prompt user to edit the given connect string
                        if ((bool)dataLinks.PromptEdit(ref oConnection))
                        {
                            connectionString =
                                 connection.ConnectionString;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }
                }
                try
                {
                    SettingsValidator.validateConnectionString(connectionString, null);
                    this.tDBConn.Text = connectionString;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
                return;
            }

            if (sender.Equals(btnSertPath) || sender.Equals(btnOutputPath) )
            {
                TextBox tb = null;
                String ext = "Certificate files (*.pfx)|*.pfx";
                Boolean folderDial = true;
                if (sender.Equals(btnSertPath))
                {
                    tb = tCertPath;
                    folderDial = false;
                }
                if (sender.Equals(btnOutputPath))
                    tb = tOutputPath;
                
                if (tb == null)
                    return;
                String path = tb.Text == String.Empty ? System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.StartupPath) : tb.Text;
                CommonDialog dialog = null;

                if (!folderDial)
                {
                    OpenFileDialog d = new OpenFileDialog();
                    dialog = d;
                    d.Title = "Please select folder";
                    d.Filter = ext;
                    d.InitialDirectory = path;

                }
                else
                {
                    FolderBrowserDialog fbd = new FolderBrowserDialog();
                    dialog = fbd;
                    fbd.SelectedPath = path;

                }

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    String result = (dialog is OpenFileDialog) ? ((OpenFileDialog)dialog).FileName : ((FolderBrowserDialog)dialog).SelectedPath;
                    if (sender.Equals(btnSertPath))
                    {
                        try
                        {
                            SettingsValidator.validateCertificate(result, tPwd.Text);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                            return;
                        }
                    }
                    tb.Text = result;
                }



                return;
            }


            if (sender.Equals(btnSave))
            {
                Properties.Settings settings = Properties.Settings.Default;


                settings.certificate_path = tCertPath.Text;
                settings.pwd = tPwd.Text;
                settings.db_connection = tDBConn.Text;
                settings.autoincrement_retrive = tIdentity.Text;
                settings.xml_output = tOutputPath.Text;
                try
                {
                    SettingsValidator.validateAndSave(settings);
                    Hide();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
            if (sender.Equals(btnClose))
            {
                Hide();
                return;
            }
        }



        private void btnDBConn_Click(object sender, EventArgs e)
        {
            callMethode(sender);
        }

        private void btnSertPath_Click(object sender, EventArgs e)
        {
            callMethode(sender);
        }

        private void btnOutputPath_Click(object sender, EventArgs e)
        {
            callMethode(sender);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            callMethode(sender);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            callMethode(sender);
        }

        private void btnLog_Click(object sender, EventArgs e)
        {
            callMethode(sender);
        }

        private void FrmSettings_Load(object sender, EventArgs e)
        {
            Properties.Settings settings = Properties.Settings.Default;
            tCertPath.Text = Path.GetFullPath(settings.certificate_path);
            tPwd.Text = settings.pwd;
            tDBConn.Text = settings.db_connection;
            tIdentity.Text = settings.autoincrement_retrive;
            tOutputPath.Text = Path.GetFullPath(settings.xml_output);
            tUrl.Text = settings.url;
        }
    }
}
